package hackathon.techbooster.kuraudokakeibo;

import java.io.InputStream;
import java.util.Calendar;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class TourokuActivity extends Activity {

	private static final String RECORD_PATH = "record.txt";
	private static final String BMP_PATH = "sample.bmp";

	private static final int REQUEST_GALLERY = 0;

	private Bitmap img;
	private int yt;
	private int mt;
	private int dt;
	private Record record;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.touroku);
		record = new Record();

		// 日付
		final Calendar now = Calendar.getInstance();
		final TextView sdate = (TextView) findViewById(R.id.sdate);
		sdate.setText(String.format("%d/%d/%d", now.get(Calendar.YEAR),
				now.get(Calendar.MONTH) + 1, now.get(Calendar.DAY_OF_MONTH)));

		// 日付選択ボタン
		Button date = (Button) findViewById(R.id.date);
		date.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {

				new DatePickerDialog(TourokuActivity.this,
						new DatePickerDialog.OnDateSetListener() {
							@Override
							public void onDateSet(DatePicker view, int year,
									int monthOfYear, int dayOfMonth) {

								sdate.setText(String.format("%d/%d/%d", year,
										monthOfYear + 1, dayOfMonth));

								yt = year;
								mt = monthOfYear + 1;
								dt = dayOfMonth;

								record.setYear(yt);
								record.setMonth(mt);
								record.setDay(dt);
							}
						}, now.get(Calendar.YEAR), now.get(Calendar.MONTH), now
								.get(Calendar.DAY_OF_MONTH)).show();
			}
		});

		// 写真選択ボタン
		Button select = (Button) findViewById(R.id.select);
		select.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.setType("image/*");
				intent.setAction(Intent.ACTION_GET_CONTENT);
				startActivityForResult(intent, REQUEST_GALLERY);
			}

		});

		// スピナー
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

		// アイテムを追加します
		adapter.add("食費");
		adapter.add("雑費");
		adapter.add("交際費");

		final Spinner bunrui = (Spinner) findViewById(R.id.bunrui);
		// アダプターを設定します
		bunrui.setAdapter(adapter);

		// 登録ボタン
		Button touroku = (Button) findViewById(R.id.touroku);
		touroku.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {

				boolean flg = true;

				// 写真のbitmap
				if (img == null) {
					//flg = false;
				} else {
					record.saveImage(getApplicationContext(), img, BMP_PATH);
					record.setImgPath(BMP_PATH);
				}

				// 分類スピナー
				String h = (String) bunrui.getSelectedItem();
				if (h == null || h.length() == 0) {
					flg = false;
				} else {
					record.setCategory(h);
				}

				// 品目テキスト
				EditText hinmoku = (EditText) findViewById(R.id.hinmoku);
				if (hinmoku.getText() != null
						&& hinmoku.getText().toString().length() != 0) {
					String ht = hinmoku.getText().toString();
					record.setName(ht);
				} else {
					flg = false;
				}

				// 金額テキスト
				EditText kingaku = (EditText) findViewById(R.id.kingaku);
				if (kingaku.getText() != null
						&& kingaku.getText().toString().length() != 0) {
					int kt = Integer.parseInt(kingaku.getText().toString());
					record.setValue(kt);
				} else {
					flg = false;
				}

				if (flg) {
					record.registRecord(getApplicationContext(), RECORD_PATH);
				} else {
					Toast.makeText(TourokuActivity.this, "入力エラー",
							Toast.LENGTH_SHORT).show();
				}
			}

		});
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		// 写真imageView
		ImageView iv = (ImageView) findViewById(R.id.receipt);

		if (requestCode == REQUEST_GALLERY && resultCode == RESULT_OK) {
			try {
				InputStream in = getContentResolver().openInputStream(
						data.getData());
				img = BitmapFactory.decodeStream(in);
				in.close();

				// 選択した画像を表示
				if (img == null) {
					iv.setVisibility(View.GONE);
				} else {
					iv.setImageBitmap(img);
					iv.setVisibility(View.VISIBLE);
				}

			} catch (Exception e) {
				iv.setVisibility(View.GONE);
			}
		}
	}
}